home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / games / seahaven / store.c++ < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  2.8 KB  |  135 lines

  1. /*
  2.  * Copyright 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. #include "seahaven.h"
  18.  
  19. struct HashBucket {
  20.   struct {
  21.     Stack s;
  22.     int y;        // hack, really want position w/in stack as an index
  23.   } cardpos[52];
  24.   HashBucket *next;
  25. };
  26.  
  27. static const int NUMHASHSLOTS = 337;
  28.  
  29. HashBucket *hashtable[NUMHASHSLOTS] = {NULL};
  30.  
  31. void
  32. InitHashTable()
  33. {
  34.     for (int i=0; i< NUMHASHSLOTS; i++) {
  35.     if (hashtable[i]) {
  36.         HashBucket *t = hashtable[i], *n;
  37.         do {
  38.         n = t->next;
  39.         delete t;
  40.         } while (t = n);
  41.         hashtable[i] = NULL;
  42.     }
  43.     }
  44. }
  45.  
  46. #ifdef notdef
  47. // the single stacks are symmetric, so store them as singlestack[0] and y=0
  48. inline void FoldStack(Stack reals, int realy, Stack &s, int &y)
  49. {
  50.     extern Stack singlestack[];
  51.     if (reals == singlestack[0] || reals == singlestack[1]
  52.         || reals == singlestack[2] || reals == singlestack[3]) {
  53.       s = singlestack[0];
  54.       y = 0;
  55.     } else {
  56.       s = reals;
  57.       y = realy;
  58.     }
  59. }
  60. #endif
  61.  
  62. // retruns true if found it
  63. Bool
  64. hashlookup(int key)
  65. {
  66.     if (!hashtable[key]) return False;
  67.  
  68.     HashBucket *t = hashtable[key];
  69.     do {
  70.     int i;
  71.     Card *c;
  72.     Bool match;
  73.  
  74.     for (i=0, c = &cards[0][0]; i < 52; i++, c++) {
  75.         match = True;
  76.         if ( (*c)->getFoldedY() != t->cardpos[i].y ||
  77.         (*c)->getFoldedStack() != t->cardpos[i].s ) {
  78.         match = False;
  79.         break;
  80.         }
  81.     }
  82.     if (match)
  83.         return True;
  84.     } while (t = t->next);
  85.  
  86.     return False;
  87.  
  88. }
  89.  
  90. // returns true if new state
  91. Bool
  92. CheckNewState()
  93. {
  94.     unsigned int hashkey = 0;
  95.  
  96.     // compute key
  97.     int i;
  98.     Card *c;
  99.     for (i=0, c = &cards[0][0]; i < 52; i++, c++) {
  100.     hashkey += ((int) (*c)->getFoldedStack() << (*c)->getValue());
  101.     }
  102.     
  103.     if (hashlookup(hashkey % NUMHASHSLOTS))
  104.     return False;
  105.     else
  106.     return True;
  107.     
  108. }
  109.  
  110. void
  111. hashinsert(int key, HashBucket *pail)
  112. {
  113.     pail->next = hashtable[key];
  114.     hashtable[key] = pail;
  115. }
  116.  
  117. void
  118. SaveState()
  119. {
  120.     
  121.     HashBucket *pail = new HashBucket;
  122.     unsigned int hashkey = 0;
  123.     
  124.     int i;
  125.     Card *c;
  126.     for (i=0, c = &cards[0][0]; i < 52; i++, c++) {
  127.     pail->cardpos[i].s = (*c)->getFoldedStack();
  128.     pail->cardpos[i].y = (*c)->getFoldedY();
  129.     hashkey += ((int) pail->cardpos[i].s << (*c)->getValue());
  130.     }
  131.     
  132.     hashinsert(hashkey % NUMHASHSLOTS, pail);
  133.     
  134. }
  135.